package com.stockz.equity.charting.decorator;

import java.awt.Dimension;

import javax.swing.JToolBar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.StockChartViewStructure;
import com.stockz.equity.pattern.Decorator;

@Component
public class StockChartViewPaintTimeDecorator implements Decorator<StockChartView> {
	@Autowired private StockChartViewStructure stockChartViewStructure;
	
	@Override
	public void decorate(StockChartView view) {
		int width = 0;
		Dimension dimension = view.getStockChartControlPanel().getPreferredSize();
		dimension.height = 0;
		boolean defaultHeightSet = false;
		
		for(java.awt.Component component : view.getStockChartControlPanel().getComponents()){
			if(component instanceof JToolBar){
				JToolBar toolbar = (JToolBar)component;
				
				toolbar.setPreferredSize(new Dimension(toolbar.getWidth(),stockChartViewStructure.getToolbarHeight()));
				
				if(!defaultHeightSet){
					dimension.height = toolbar.getHeight();
					defaultHeightSet = true;
				}
				
				width += toolbar.getWidth()+8;
				if(width >= dimension.width){
					width = 0;
					dimension.height += toolbar.getHeight()+10;
				}
				
			}
		}
		
		view.getStockChartControlPanel().setPreferredSize(dimension);
		view.getStockChartControlPanel().setPreferredSize(new Dimension(view.getStockChartControlPanel().getPreferredSize().width, dimension.height));
	}
}
